﻿using EOS.IDAL;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EOS.DAL
{
    public class CommonRepository<T> : ICommonnRepository
    {

        public SqlDatabase db;

        public CommonRepository()
        {
            this.db = new DAL.SqlDatabase() ; 
        }

        #region 事物提交
        public ICommonnRepository BeginTrans()
        {
            db.BeginTrans();
            return this;
        }
        public void Commit()
        {
            db.Commit();
        }
        public void Rollback()
        {
            db.Rollback();
        }
        #endregion

        #region 执行 SQL 语句
        public int ExecuteBySql(string strSql)
        {
            return db.ExecuteBySql(strSql);
        }
        public int ExecuteBySql(string strSql, params DbParameter[] dbParameter)
        {
            return db.ExecuteBySql(strSql, dbParameter);
        }
        public int ExecuteByProc(string procName)
        {
            return db.ExecuteByProc(procName);
        }
        public int ExecuteByProc(string procName, params DbParameter[] dbParameter)
        {
            return db.ExecuteByProc(procName, dbParameter);
        }
        #endregion

        public void ExecuteByTrans(T paramT, params Action<T>[] actions)
        {
            this.BeginTrans();
            try
            {
                foreach (Action<T> action in actions)
                {
                    action(paramT);
                }
                this.Commit();
            }
            catch (Exception)
            {
                this.Rollback();
                throw;
            }
        }
    }
}
